package com.gouzheng.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor  implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
       String uri=request.getRequestURI();
       if(uri.indexOf("/user/login")>=0){
           return true;
       }
        if(uri.indexOf("/static")>=0){
            return true;
        }
        if(uri.indexOf("/user/code")>=0){
            return true;
        }
        if(uri.indexOf("/user/addUser")>=0){
            return true;
        }


        HttpSession session=request.getSession();
       if(session.getAttribute("user")!=null){
           //登录成功不拦截
           return true;
       }else {
           //拦截进入登陆页面
           response.sendRedirect("/index.jsp");
            return false;
       }
    }
}
